{% extends 'base.html' %}
{% load static %}

{% block title %}文章列表 - TechSpace{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="row">
        <!-- 主要内容区域 -->
        <div class="col-lg-8">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="display-6 fw-bold">
                    {% if current_category %}
                        分类：{{ current_category }}
                    {% elif current_tag %}
                        标签：{{ current_tag }}
                    {% elif search_query %}
                        搜索结果：{{ search_query }}
                    {% else %}
                        所有文章
                    {% endif %}
                </h1>
                <span class="text-muted">共 {{ page_obj.paginator.count }} 篇文章</span>
            </div>
            
            <!-- 文章列表 -->
            {% for article in page_obj %}
            <article class="article-card mb-4">
                <div class="row">
                    {% if article.featured_image %}
                    <div class="col-md-4">
                        <img src="{{ article.featured_image.url }}" alt="{{ article.title }}" 
                             class="img-fluid rounded" style="height: 200px; object-fit: cover;">
                    </div>
                    <div class="col-md-8">
                    {% else %}
                    <div class="col-12">
                    {% endif %}
                        <div class="article-meta mb-2">
                            <i class="fas fa-calendar"></i> {{ article.created_at|date:"Y-m-d" }}
                            <span class="ms-3">
                                <i class="fas fa-user"></i> {{ article.author.username }}
                            </span>
                            <span class="ms-3">
                                <i class="fas fa-eye"></i> {{ article.views }}
                            </span>
                            {% if article.category %}
                            <span class="ms-3">
                                <i class="fas fa-folder"></i> 
                                <a href="{% url 'blog:category_detail' article.category.slug %}" 
                                   class="text-decoration-none">{{ article.category.name }}</a>
                            </span>
                            {% endif %}
                        </div>
                        
                        <h2 class="article-title">
                            <a href="{{ article.get_absolute_url }}">{{ article.title }}</a>
                        </h2>
                        
                        <p class="article-excerpt">{{ article.excerpt }}</p>
                        
                        <div class="d-flex justify-content-between align-items-center">
                            <div class="article-tags">
                                {% for tag in article.tags.all %}
                                <a href="{% url 'blog:tag_detail' tag.slug %}" class="tag text-decoration-none">
                                    {{ tag.name }}
                                </a>
                                {% endfor %}
                            </div>
                            
                            <a href="{{ article.get_absolute_url }}" class="btn btn-sm btn-outline-primary">
                                阅读更多 <i class="fas fa-arrow-right"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </article>
            {% empty %}
            <div class="text-center py-5">
                <i class="fas fa-file-alt" style="font-size: 4rem; color: var(--gray-text);"></i>
                <h3 class="mt-3">暂无文章</h3>
                <p class="text-muted">
                    {% if search_query %}
                        没有找到包含 "{{ search_query }}" 的文章
                    {% else %}
                        敬请期待精彩内容
                    {% endif %}
                </p>
            </div>
            {% endfor %}
            
            <!-- 分页 -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="文章分页">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page=1{% if search_query %}&search={{ search_query }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if current_tag %}&tag={{ current_tag }}{% endif %}">
                            <i class="fas fa-angle-double-left"></i>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if current_tag %}&tag={{ current_tag }}{% endif %}">
                            <i class="fas fa-angle-left"></i>
                        </a>
                    </li>
                    {% endif %}
                    
                    {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if current_tag %}&tag={{ current_tag }}{% endif %}">{{ num }}</a>
                    </li>
                    {% endif %}
                    {% endfor %}
                    
                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if current_tag %}&tag={{ current_tag }}{% endif %}">
                            <i class="fas fa-angle-right"></i>
                        </a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if search_query %}&search={{ search_query }}{% endif %}{% if current_category %}&category={{ current_category }}{% endif %}{% if current_tag %}&tag={{ current_tag }}{% endif %}">
                            <i class="fas fa-angle-double-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
        
        <!-- 侧边栏 -->
        <div class="col-lg-4">
            <!-- 搜索框 -->
            <div class="tech-card mb-4">
                <h5 class="card-title mb-3">
                    <i class="fas fa-search"></i> 搜索文章
                </h5>
                <form method="get" action="{% url 'blog:article_list' %}">
                    <div class="input-group">
                        <input type="text" class="form-control" name="search" 
                               placeholder="输入关键词..." value="{{ search_query|default:'' }}">
                        <button class="btn btn-outline-primary" type="submit">
                            <i class="fas fa-search"></i>
                        </button>
                    </div>
                </form>
            </div>
            
            <!-- 分类列表 -->
            {% if categories %}
            <div class="tech-card mb-4">
                <h5 class="card-title mb-3">
                    <i class="fas fa-folder"></i> 文章分类
                </h5>
                <div class="list-group list-group-flush">
                    {% for category in categories %}
                    <a href="{% url 'blog:category_detail' category.slug %}" 
                       class="list-group-item list-group-item-action bg-transparent border-0 text-light">
                        {{ category.name }}
                        <span class="badge bg-primary rounded-pill float-end">
                            {{ category.article_set.count }}
                        </span>
                    </a>
                    {% endfor %}
                </div>
            </div>
            {% endif %}
            
            <!-- 标签云 -->
            {% if tags %}
            <div class="tech-card mb-4">
                <h5 class="card-title mb-3">
                    <i class="fas fa-tags"></i> 热门标签
                </h5>
                <div class="tag-cloud">
                    {% for tag in tags %}
                    <a href="{% url 'blog:tag_detail' tag.slug %}" class="tag text-decoration-none me-2 mb-2 d-inline-block">
                        {{ tag.name }}
                    </a>
                    {% endfor %}
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}